import { Loading } from "element-ui";

let loading;
function startLoading() {
  loading = Loading.service({
    lock: true,
    text: "加载中",
    spinner: "el-icon-loading",
    background: "rgba(0, 0, 0, 0.2)",
  });
}

function endLoading() {
  loading?.close();
}

let needLoadingRequestCount = 0;
const showFullScreenLoading = function () {
  if (needLoadingRequestCount === 0) {
    startLoading();
  }
  needLoadingRequestCount++;
};

const tryHideFullScreenLoading = function () {
  if (needLoadingRequestCount <= 0) return;
  needLoadingRequestCount--;
  if (needLoadingRequestCount === 0) {
    endLoading();
  }
};

export {
  startLoading,
  endLoading,
  showFullScreenLoading,
  tryHideFullScreenLoading,
};
